// 监听元素事件
export function on(
  element: HTMLElement | Document | Window,
  event: string,
  handler: EventListenerOrEventListenerObject,
  useCapture = false,
): void {
  if (element && event && handler) {
    element.addEventListener(event, handler, useCapture);
  }
}

// 解绑元素事件
export function off(
  element: HTMLElement | Document | Window,
  event: string,
  handler: EventListenerOrEventListenerObject,
  useCapture = false,
): void {
  if (element && event && handler) {
    element.removeEventListener(event, handler, useCapture);
  }
}

// 判断是否是火狐浏览器
export function isFirefox(): boolean {
  return !!window.navigator.userAgent.match(/firefox/i);
}

// 定义一个参数为任何类型和数量且返回类型为 T 的函数声明
export type AnyFunction<T> = (...args: any[]) => T;
export function rafThrottle<T extends AnyFunction<any>>(fn: T): AnyFunction<void> {
  let locked = false;
  return function (...args: any[]) {
    if (locked) return;
    locked = true;
    window.requestAnimationFrame(() => {
      // fn.apply(this, args);
      fn.apply(null, args);
      locked = false;
    });
  };
}
